package SEApp.boxes.albums
{
	import SEApp.se_app;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_hscrollArea;
	import UIFree.controls.uif_tabItem;
	import UIFree.controls.uif_tabsBox;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class se_abox extends uif_tabsBox {
		protected var _itemsLayer: uif_hscrollArea;
		
		public function se_abox(parent:uif_component, list: Array, ax:int=0, ay:int=0, 
								awidth:int=0, aheight:int=0, atabwidth:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, atabwidth, styleClass);
			tabs.addEventListener(Event.SELECT, function(e: Event): void {
				doChangeSelect(tabs.selectIndex);
			});
			
			var inner: Rectangle = layer.getInner();
			_itemsLayer = createItemsLayer(inner);
			
			_itemsLayer.addEventListener(Event.SELECT, function(e: Event): void {
				if (_itemsLayer.selectItem) dispatchEvent(new ObjectEvent(Event.SELECT, _itemsLayer.selectItem.control));
			});
			_itemsLayer.addEventListener(Event.CHANGE, function(e: Event): void {
				if (_itemsLayer.selectItem) dispatchEvent(new ObjectEvent(Event.CHANGE, _itemsLayer.selectItem.control));
			});
			assignTabs(list);
		}
		
		protected function createItemsLayer(inner: Rectangle): uif_hscrollArea {
			return new uif_hscrollArea(layer, inner.x, inner.y, inner.width, inner.height, style['itemslayer_class'], 27);
		}
		
		public function assignTabs(list: Array): void {
			if (_itemsLayer != null) _itemsLayer.scrollArea.disposeChilds();
			var mwidth: int = maxWidthTab(list);
			
			tabs.assignList(list, mwidth + 20, function(_scroll: uif_component, inner: Rectangle, i: int): uif_tabItem {
				return createItem(list, inner, i, style['tabitem_class'])
			});
		}
		
		public function get itemsLayer(): uif_hscrollArea {
			return _itemsLayer;
		}
		
		protected function maxWidthTab(list: Array): int {
			var itStyle: Object = app.styleByName(style['tabitem_class']); 
			return uif_component.maxWidth(list, itStyle.text_format.font, itStyle.text_format.size);
		}
		
		protected function createItem(list: Array, inner: Rectangle, index: int, styleName: String): uif_tabItem {
			inner.inflate(8, 0);
			inner.x += 8;
			var item: uif_tabItem = new uif_tabItem(tabs.scrollArea, inner, styleName);
			item.text = getItemTitle(list[index]);
			return item; 
		}
		
		protected function get seApp(): se_app {
			return app as se_app;
		}
		
		protected function doChangeSelect(tabIndex: int): void {
		}		
	}
}